from django.shortcuts import render
from django.http import JsonResponse
from django.db import connection
from django.core.cache import cache


def health_check(request):
    """健康检查端点"""
    try:
        # 检查数据库连接
        with connection.cursor() as cursor:
            cursor.execute("SELECT 1")

        # 检查Redis连接（如果配置了）
        try:
            cache.set('health_check', 'ok', 10)
            cache.get('health_check')
        except Exception:
            pass  # Redis不是必需的

        return JsonResponse({
            'status': 'healthy',
            'database': 'ok',
            'cache': 'ok'
        })
    except Exception as e:
        return JsonResponse({
            'status': 'unhealthy',
            'error': str(e)
        }, status=500)

# Create your views here.
